package com.itheima.springbootmybatis.controller;

import com.itheima.springbootmybatis.pojo.*;
import com.itheima.springbootmybatis.service.ArticleService;
import jdk.jshell.Snippet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    //新增商品
    @PostMapping
    public Result add(@RequestBody @Validated FruitVegetables fruitVegetables){
        articleService.add(fruitVegetables);
        return Result.success();
    }
    //商品列表（条件分页）
    @GetMapping
    public Result<PageBean<FruitVegetables>>list(
            Integer pageNum,
            Integer pageSize,

            @RequestParam(required = false)String origin,
            @RequestParam(required = false)String fruitName
    ){
      PageBean<FruitVegetables>pb=  articleService.list(pageNum,pageSize,origin,fruitName);
  return Result.success(pb);
    }
//根据id查询商品
    @GetMapping("/detail")
    public Result<FruitVegetables> detail(Integer id){
        FruitVegetables a=articleService.findById(id);
        return Result.success(a);
    }
    //更新商品
    @PutMapping
    public Result update(@RequestBody @Validated FruitVegetables fruitVegetables){
        String a=articleService.update(fruitVegetables);
        if (a==null){
        return Result.error("这不是您的商品，您无权修改！");
        }else{
            return Result.success();
        }
    }

    //删除商品
    @DeleteMapping
    public Result delete(Integer id){
       String a=articleService.deleteById(id);
        if (a==null){
            return Result.error("这不是您的商品，您无权删除！");
        }else{
            return Result.success();
        }
    }
}
